package io.renren.modules.mvs.controller;

import io.renren.common.annotation.SysLog;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;
import io.renren.modules.mvs.dto.PaymentDetailDto;
import io.renren.modules.mvs.entity.PaymentEntity;
import io.renren.modules.mvs.entity.PaymentDetailEntity;
import io.renren.modules.mvs.service.PaymentDetailService;
import io.renren.modules.mvs.service.PaymentService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.Map;



/**
 * 缴纳记录表
 *
 * @author chenshun
 * @email sunlightcs@gmail.com
 * @date 2025-03-30 17:08:24
 */
@RestController
@RequestMapping("mvs/payment")
public class PaymentController {
    @Autowired
    private PaymentService paymentService;
    @Autowired
    private PaymentDetailService paymentDetailService;

    /**
     * 列表
     */
    @GetMapping("/list")
    @RequiresPermissions("mvs:payment:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = paymentService.queryPage(params);
        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @GetMapping("/info/{id}")
    @RequiresPermissions("mvs:payment:info")
    public R info(@PathVariable("id") Integer id){
		PaymentEntity payment = paymentService.getById(id);

        return R.ok().put("payment", payment);
    }

    /**
     * 保存
     */
    @SysLog("新增缴纳记录")
    @PostMapping("/save")
    @RequiresPermissions("mvs:payment:save")
    public R save(@RequestBody PaymentEntity payment){
		paymentService.save(payment);

        return R.ok();
    }

    /**
     * 修改
     */
    @SysLog("修改缴纳记录")
    @PutMapping("/update")
    @RequiresPermissions("mvs:payment:update")
    public R update(@RequestBody PaymentEntity payment){
		paymentService.updateById(payment);

        return R.ok();
    }

    /**
     * 删除
     */
    @SysLog("删除缴纳记录")
    @DeleteMapping("/delete")
    @RequiresPermissions("mvs:payment:delete")
    @Transactional(rollbackFor = Exception.class)
    public R delete(@RequestBody Integer[] ids){
        //1.先删除缴纳记录关联的缴费日志表的数据
        paymentDetailService.lambdaUpdate().in(PaymentDetailEntity::getPaymentId, Arrays.asList(ids)).remove();
        //2.再删除相应的缴纳记录
		paymentService.removeByIds(Arrays.asList(ids));
        return R.ok();
    }

    /**
     * 根据日期范围获取每日的总缴费金额
     */
    @GetMapping("/list/money")
    @RequiresPermissions("mvs:payment:list")
    public R moneyListByDateRange(@RequestParam(required = false) String startDate, @RequestParam(required = false) String endDate) {
        Map<String, Collection<?>> dataMap = paymentService.getMoneyListByDateRange(startDate, endDate);
        return R.ok().put("dataMap", dataMap);
    }

    /**
     * 根据缴纳记录id获取缴纳详情列表
     */
    @GetMapping("/list/detail/{id}")
    @RequiresPermissions("mvs:payment:list")
    public R detailList(@PathVariable Long id) {
        List<PaymentDetailDto> detailList = paymentService.getDetailList(id);
        return R.ok().put("detailList", detailList);
    }

    /**
     * 根据罚单编号获取缴纳详情列表
     */
//    @GetMapping("/list/log/{id}")
//    @RequiresPermissions("mvs:payment:list")
//    public R

}
